//
//  AppNetworkingManager+OAOrganizeFramework.m
//  OANetworkManager_Example
//
//  Created by TFAppleWork-Summer on 2018/2/22.
//  Copyright © 2018年 GuoRui. All rights reserved.
//

#import "AppNetworkingManager+OADepartment.h"
#import "OAUserModel.h"
#import "OADepartmentModel.h"

@implementation AppNetworkingManager (OADepartment)
+(void)getDepartNeedUpdate:(OANetworkingObjectBlock)updateTimeBlock
                  complete:(OANetworkingObjectBlock)complete{
    // 1 调用组织架构更新时间
    [self getUpdateTimeComplete:^(id object, NSTimeInterval date,NSError *error) {
        if (error) {
            updateTimeBlock(object,error);
            return ;
        }
        if ([object boolValue])
        {// 需要更新
            updateTimeBlock(@(YES),nil);
            // 2 请求最新的组织架构
            [self getDeptWithDate:date
                         complete:^(id object, NSError *error) {
                             if (error)
                             {
                                 complete(object,error);
                                 return ;
                             }
                             if ([object boolValue]) {
                                 complete(@(YES),nil);
                             }
                         }];
        }
        else{
            // 不需要更新
            updateTimeBlock(@(NO),nil);
        }
    }];
    
}

/**
 请求最新更新时间

 @param complete 更新时间的回调
 */
+ (void)getUpdateTimeComplete:(void(^)(id object ,NSTimeInterval date ,NSError *error))complete{
    [self startRequestWithParams:nil
                            path:@"dept/updateDate"
              responseObjectPath:@""
           popoverProgressString:nil
        handleErrorAutomatically:OANETWORKMANAGERSHOWHUD
                   responseBlock:^(id response, NSError *error) {
                       if (error) {
                           complete(@(NO),0,error);
                           return ;
                       }
                       NSTimeInterval date = [response[@"data"] doubleValue];
                       NSTimeInterval oldDate = [[NSUserDefaults standardUserDefaults] doubleForKey:OANETWORKMANAGERDEPARTMENT_KET];
                       if (date != oldDate)
                       {
                           complete(@(YES),date,nil);
                       }
                       else
                       {
                           complete(@(NO),0,nil);
                       }
                       
        }];
}

/**
 请求组织架构

 @param date 最新时间
 @param complete 回调
 */
+(void)getDeptWithDate:(NSTimeInterval)date complete:(OANetworkingObjectBlock)complete{
    [self startRequestWithParams:nil
                            path:@"dept/get"
              responseObjectPath:@"data"
           popoverProgressString:nil
        handleErrorAutomatically:OANETWORKMANAGERSHOWHUD
                   responseBlock:^(id response, NSError *error) {
                       if (!error)
                       {
                           // 1. 删除之前的本地表
                           [OADepartmentModel deleteTab];
                           [OAUserModel deleteTab];
                           
                           NSArray *departmentArray = [response objectForKey:@"departmentTab"];
                           NSArray *departmentModels = [OADepartmentModel modelWithArray:departmentArray];
                           [departmentModels makeObjectsPerformSelector:@selector(save)];

                           NSArray *userArray= [response objectForKey:@"userTab"];
                           NSArray *userModels = [OAUserModel modelWithArray:userArray];
                           [userModels makeObjectsPerformSelector:@selector(save)];
                           
                           complete(@(YES),nil);
                           [[NSUserDefaults standardUserDefaults] setDouble:date forKey:OANETWORKMANAGERDEPARTMENT_KET];
                       }
                       else{
                           complete(@(NO),error);
                       }
                       
        }];
}

@end
